package priv.chenjh.filedownload.web.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author chenjh
 * @since 2020/3/4 11:56
 */
@Controller
@RequestMapping("/files")
public class FileDownloadController {

    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        InputStream is = new ClassPathResource("files/2019年度培训汇总-研发中心.xlsx").getInputStream();
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("2019年度培训汇总-研发中心.xlsx","UTF-8"));
        response.setContentType("application/octet-stream");
        StreamUtils.copy(is, response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

}
